import { createWebHistory, createRouter } from 'vue-router'

import home from '@/views/home/index.vue'
import login from '@/views/login.vue'
import userinfo from '@/views/home/userInfo.vue'
import students_mange from '@/views/home/studentsMange.vue'
import teachers_mange from '@/views/home/teachersMange.vue'
import achievement_mange from '@/views/home/achievementMange.vue'
import review_mange from '@/views/home/reviewMange.vue'

const routes = [
  { path: '/', redirect: '/home' },
  
  { path: '/login',
    component: login,
  },

  { path: '/home',
    component: home,
    redirect: '/home/userinfo',
    children: [
      {
        path: 'userinfo',
        component: userinfo
      },
      {
        path: 'students_mange',
        component: students_mange
      },
      {
        path: 'teachers_mange',
        component: teachers_mange
      },
      {
        path: 'achievement',
        component: achievement_mange
      },
      {
        path: 'review',
        component: review_mange
      }
    ]
  }
  
]

export const router = createRouter({
  history: createWebHistory(),
  routes,
})

// 添加路由守卫
router.beforeEach((to, from, next) => {
  // console.log("路由守卫执行了一次")
  if (to.path !== '/login' && localStorage.getItem("token") === null) {
    console.log('用户未登录，请先登录');
    next('/login');
  } else {
    next();
  }
});